<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>const总结 | hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="常量不可再次更改变量值 1const int a = 1; 常成员函数不可修改类成员变量 123456789101112131415161718class A&amp;#123;    private:        int a;    public:        void f1(int arg) const// 常成员函数        &amp;#123;            cout &amp;lt;&amp;lt;">
<meta property="og:type" content="article">
<meta property="og:title" content="const总结">
<meta property="og:url" content="http://yoursite.com/2019/07/22/编程语言/c++/const总结/index.html">
<meta property="og:site_name" content="hexo">
<meta property="og:description" content="常量不可再次更改变量值 1const int a = 1; 常成员函数不可修改类成员变量 123456789101112131415161718class A&amp;#123;    private:        int a;    public:        void f1(int arg) const// 常成员函数        &amp;#123;            cout &amp;lt;&amp;lt;">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2019-07-25T06:41:18.935Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="const总结">
<meta name="twitter:description" content="常量不可再次更改变量值 1const int a = 1; 常成员函数不可修改类成员变量 123456789101112131415161718class A&amp;#123;    private:        int a;    public:        void f1(int arg) const// 常成员函数        &amp;#123;            cout &amp;lt;&amp;lt;">
  
    <link rel="alternate" href="/atom.xml" title="hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.ico">
  
  
    
  
  <link rel="stylesheet" href="/css/style.css">
  

</head>
</html>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    
    <div id="header-inner" class="inner">
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">首页</a>
        
          <a class="main-nav-link" href="/archives">归档</a>
        
          <a class="main-nav-link" href="/categories/高等数学">高等数学</a>
        
          <a class="main-nav-link" href="/categories/线性代数">线性代数</a>
        
          <a class="main-nav-link" href="/categories/数据结构与算法">数据结构与算法</a>
        
          <a class="main-nav-link" href="/categories/English">英语</a>
        
          <a class="main-nav-link" href="/about">关于</a>
        
      </nav>
      
    </div>
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">hexo</a>
      </h1>
      
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-编程语言/c++/const总结" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2019/07/22/编程语言/c++/const总结/" class="article-date">
  <time datetime="2019-07-22T09:00:28.000Z" itemprop="datePublished">2019-07-22</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/编程语言/">编程语言</a>►<a class="article-category-link" href="/categories/编程语言/c/">c++</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      const总结
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <!-- Table of Contents -->
        
        <h2 id="常量"><a href="#常量" class="headerlink" title="常量"></a>常量</h2><p>不可再次更改变量值</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">int</span> a = <span class="number">1</span>;</span><br></pre></td></tr></table></figure>
<h2 id="常成员函数"><a href="#常成员函数" class="headerlink" title="常成员函数"></a>常成员函数</h2><p>不可修改类成员变量</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">private</span>:</span><br><span class="line">        <span class="keyword">int</span> a;</span><br><span class="line">    <span class="keyword">public</span>:</span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">f1</span><span class="params">(<span class="keyword">int</span> arg)</span> <span class="keyword">const</span><span class="comment">// 常成员函数</span></span></span><br><span class="line"><span class="function">        </span>&#123;</span><br><span class="line">            <span class="built_in">cout</span> &lt;&lt; <span class="string">"variable :"</span>;</span><br><span class="line">            <span class="built_in">cout</span> &lt;&lt; arg &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">            <span class="comment">// a = 100; //error</span></span><br><span class="line">            <span class="built_in">cout</span> &lt;&lt; a &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">void</span> <span class="title">f2</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> i)</span> </span>&#123;</span><br><span class="line">            <span class="comment">// i = 10000;//error  </span></span><br><span class="line">            <span class="built_in">cout</span> &lt;&lt; i &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="限定不可修改函数参数"><a href="#限定不可修改函数参数" class="headerlink" title="限定不可修改函数参数"></a>限定不可修改函数参数</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">f2</span><span class="params">(<span class="keyword">const</span> <span class="keyword">int</span> i)</span> </span>&#123;</span><br><span class="line">       i = <span class="number">10000</span>; <span class="comment">// error</span></span><br><span class="line">       <span class="built_in">cout</span> &lt;&lt; i &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<h2 id="限定const对象只能调用const成员函数"><a href="#限定const对象只能调用const成员函数" class="headerlink" title="限定const对象只能调用const成员函数"></a>限定const对象只能调用const成员函数</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> A obj2;</span><br><span class="line">obj2.f1(<span class="number">999</span>);</span><br><span class="line"><span class="comment">// obj2.f2(666);// error !!!</span></span><br></pre></td></tr></table></figure>
<h2 id="限定不可更改的指针"><a href="#限定不可更改的指针" class="headerlink" title="限定不可更改的指针"></a>限定不可更改的指针</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i = <span class="number">1000</span>;</span><br><span class="line"><span class="keyword">int</span> *<span class="keyword">const</span> p2 = &amp;i;</span><br><span class="line"><span class="built_in">cout</span> &lt;&lt; *p2 &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line"><span class="keyword">int</span> j = <span class="number">300</span>;</span><br><span class="line"><span class="comment">// p2 = &amp;j; // error !!!!!!!</span></span><br><span class="line"><span class="comment">// cout &lt;&lt; *p2 &lt;&lt; endl;</span></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2019/07/22/编程语言/c++/const总结/" data-id="cjz7utmp6002mxhpelybi9jfk" class="article-share-link">分享</a>
      
      
      
    </footer>
  </div>
  
    
    
<nav id="article-nav">
  
    <a href="/2019/07/29/English/Words/theme-words/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          theme_words
        
      </div>
    </a>
  
  
    <a href="/2019/07/20/English/Writer/quotes/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">quotes</div>
    </a>
  
</nav>

  
</article>
 
     
  

</section>
           
    <aside id="sidebar">
  
    

  
    
    <div class="widget-wrap">
    
      <div class="widget" id="toc-widget-fixed">
      
        <strong class="toc-title">文章目录</strong>
        <div class="toc-widget-list">
              <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#常量"><span class="toc-number">1.</span> <span class="toc-text">常量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#常成员函数"><span class="toc-number">2.</span> <span class="toc-text">常成员函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#限定不可修改函数参数"><span class="toc-number">3.</span> <span class="toc-text">限定不可修改函数参数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#限定const对象只能调用const成员函数"><span class="toc-number">4.</span> <span class="toc-text">限定const对象只能调用const成员函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#限定不可更改的指针"><span class="toc-number">5.</span> <span class="toc-text">限定不可更改的指针</span></a></li></ol>
          </div>
      </div>
    </div>

  
    

  
    
  
    
  
    

  
    
  
    <!--微信公众号二维码-->


  
</aside>

      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-left">
      &copy; 2014 - 2019 tiger&nbsp;|&nbsp;
      主题 <a href="https://github.com/giscafer/hexo-theme-cafe/" target="_blank">Cafe</a>
    </div>
     <div id="footer-right">
      联系方式&nbsp;|&nbsp;375478250@qq.com
    </div>
  </div>
</footer>
 <script src="/jquery/jquery.min.js"></script>

 <script src='https://unpkg.com/mermaid@8.1.0/dist/mermaid.min.js'></script>
 <script>
 if (window.mermaid) {
	 mermaid.initialize({startOnLoad:true});
 }
</script>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">首页</a>
  
    <a href="/archives" class="mobile-nav-link">归档</a>
  
    <a href="/categories/高等数学" class="mobile-nav-link">高等数学</a>
  
    <a href="/categories/线性代数" class="mobile-nav-link">线性代数</a>
  
    <a href="/categories/数据结构与算法" class="mobile-nav-link">数据结构与算法</a>
  
    <a href="/categories/English" class="mobile-nav-link">英语</a>
  
    <a href="/about" class="mobile-nav-link">关于</a>
  
</nav>
    <img class="back-to-top-btn" src="/images/fly-to-top.png"/>
<script>
// Elevator script included on the page, already.
window.onload = function() {
  var elevator = new Elevator({
    selector:'.back-to-top-btn',
    element: document.querySelector('.back-to-top-btn'),
    duration: 1000 // milliseconds
  });
}
</script>
    <!-- author:forvoid begin -->
<!-- author:forvoid end -->


  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      })
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      })
    </script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>
  


 <script src="/js/is.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/script.js"></script>
<script src="/js/elevator.js"></script>
  </div>
</body>
</html>
